#include "bmcxcpu.h"

#include <httpclient.h>

BmcxCpu::BmcxCpu() {
    QString url = "https://cpu.bmcx.com/web_system/bmcx_com_www/system/file/cpu/get_data/?lx=cpu&s=1&e=999999999&ajaxtimestamp=1725700801011";

    m_cpus = HttpClient::instance().get(url).toList();
}

QString BmcxCpu::name()
{
    return "便民查询 - CPU";
}

QString BmcxCpu::prefix()
{
    return "https://cpu.bmcx.com/";
}

QList<QListWidgetItem *> BmcxCpu::searching_item(const QString &text)
{
    QList<QListWidgetItem *> list;

    foreach (auto cpu, m_cpus) {
        auto var = cpu.toMap();
        auto id = var.value("id").toInt();
        auto pai_ming = var.value("pai_ming").toInt();
        auto ming_cheng = var.value("ming_cheng").toString();
        auto shu_zhi = var.value("shu_zhi").toString();
        auto bai_fen_bi = var.value("bai_fen_bi").toInt();

        if (text.isEmpty() || ming_cheng.contains(text, Qt::CaseInsensitive)) {
            auto v = newItem();
            v->setText(QString("%1: %2").arg(pai_ming).arg(ming_cheng));
            v->setData(990, id);
            list << v;
        }
    }
    return list;
}

QString BmcxCpu::content_item(const QListWidgetItem *item)
{
    QString url = prefix() + item->data(990).toString() + "__cpu/";
    // return HttpClient::instance().get(url).content();
    return url;
}
